package com.dongzhu1428.express.module1.rest;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dongzhu1428.express.TIP;
import com.dongzhu1428.express.module1.Result;
import com.dongzhu1428.express.module1.dto.AcceptTaskParamDto;
import com.dongzhu1428.express.module1.dto.ReleaseTaskParamDto;
import com.dongzhu1428.express.module1.dto.controllerDto.Task.FinishTaskManuallyParamDto;
import com.dongzhu1428.express.module1.entity.DeliveryTask;

import com.dongzhu1428.express.module1.entity.Express;
import com.dongzhu1428.express.module1.mapper.express.DeliveryTaskMapper;
import com.dongzhu1428.express.module1.mapper.express.ExpressMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import  com.dongzhu1428.express.module1.service.*;
import java.sql.Timestamp;
import java.util.List;
/**
 * 任务模块
 */
@RestController
@RequestMapping("/task")
public class TaskController {

	@Autowired
	DeliveryTaskMapper deliveryTaskMapper;

	@Autowired
	ExpressMapper expressMapper;

	@Autowired
	TaskService taskService;



	/**
	 * 获取任务广场中的任务
	 * @return
	 */
	@GetMapping("/getTaskAvailable")
	public Result<List<DeliveryTask>> getTaskList(){
		System.out.println();
		List<DeliveryTask> deliveryTasks = deliveryTaskMapper.selectList(new QueryWrapper<DeliveryTask>().eq("state", 0));
		deliveryTasks.sort((t1,t2)-> (int)(t2.getCreateTime().getTime()-t1.getCreateTime().getTime()));
		return 	Result.success(deliveryTasks);
	}

	/**
	 * 发布任务
	 * @param dto
	 * @return
	 */
	@PostMapping("releaseTask")
	public Result<TIP> releaseTask(@RequestBody ReleaseTaskParamDto dto){
		DeliveryTask task = DeliveryTask.builder()
				.expressStationId(dto.getExpressStationId())
				.address(dto.getAddress())
				.comment(dto.getComment())
				.doorTime(new Timestamp(DateUtil.parseDate(dto.getDoorTime()).getTime()))
				.expressNum(dto.getExpressNum())
				.createTime(new Timestamp(DateUtil.date().getTime()))
				.reward(dto.getReward())
				.userId(dto.getUserId())
				.weight(dto.getWeight())
				.value(dto.getValue())
				.build();
		deliveryTaskMapper.insert(task);
		expressMapper.update(null,new UpdateWrapper<Express>().in("id",dto.getExpressIdList()).set("task_id",task.getId()));
		return Result.success(null,"发布任务成功");
	}


	/**
	 * 接受任务
	 * @param dto
	 * @return
	 */
	@PostMapping("acceptTask")
	public Result<TIP> acceptTask(@RequestBody AcceptTaskParamDto dto){
		deliveryTaskMapper.update(null,new UpdateWrapper<DeliveryTask>().eq("id",dto.getTaskId())
				.set("courier_id",dto.getDeliverymanId())
						.set("state",1)
				.set("accept_time",new Timestamp(DateUtil.date().getTime())));
		return Result.success(null,"接受任务成功");
	}

	/**
	 * 获取指定任务的快递列表
	 * @param taskId
	 * @return
	 */
	@GetMapping("getExpressList")
	public Result<List<Express>> getExpressList(@RequestParam("taskId") int taskId){
		List<Express> expressList = expressMapper.selectList(new QueryWrapper<Express>().eq("task_id", taskId));
		return Result.success(expressList);

	}


	/**
	 * 得到任务
	 *
	 * @param isDeliveryman @require 是否是送货人
	 * @param keyId         用户或者快递员id
	 * @param state        任务状态
	 * @return {@link Result}<{@link List}<{@link DeliveryTask}>>
	 */
	@GetMapping("/getTasks")
	public Result<List<DeliveryTask>> getTasks(@RequestParam boolean isDeliveryman,@RequestParam int keyId,@RequestParam int  state){
		return  taskService.getTaskOfUser(isDeliveryman,keyId,state);

	}

	/**
	 * 完成任务
	 * @param taskId 任务id
	 * @return {@link Result}<{@link String }>
	 *
	 */
	@PostMapping("finishTask")
	public  Result<TIP> finishTask(@RequestParam int taskId){
		return  taskService.finishedTask(taskId);
}

	/**
	 * 取消任务
	 *
	 * @param taskId 任务id
	 * @return {@link Result}<{@link TIP}>
	 */
	@PostMapping("cancelTask")
	public  Result<TIP> cancelTask(@RequestParam int taskId){
		return  taskService.cancelTask(taskId);
	}


	/**
	 * 快递员手动完成任务
	 *
	 * @param dto dto
	 * @return {@link Result}<{@link TIP}>
	 */
	@PostMapping("finishTaskManually")
	public  Result<TIP> finishTaskManually(@ModelAttribute FinishTaskManuallyParamDto  dto){
		return  taskService.finishTaskManually(dto);
	}


	/**
	 * 确认快速未送达
	 *
	 * @param taskId 任务id
	 * @return {@link Result}<{@link TIP}>
	 */
	@PostMapping("/undelivered")
	public  Result<TIP>  undelivered( int taskId){
		return  taskService.undelivered(taskId);
	}

}
